package com.net.xpay.admin.controller.admin;

import com.net.common.model.ResultPagedList;
import com.net.xpay.admin.vo.PartnerInsiderSettleLogVo;
import com.net.xpay.common.domain.partner.PartnerInsiderSettleLog;
import com.net.xpay.common.manager.partner.PartnerInsiderSettleLogManager;
import com.net.xpay.core.model.PartnerInsiderSettleLogSearchModel;
import com.net.xpay.core.utils.BeanCopierUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.List;

@RestController
@RequestMapping("/admin/partner_insider_settle_log")
@Api(tags = "后台/内部员工结算", description = "后台/内部员工结算")
public class AdminPartnerInsiderSettleLogController {

    @Autowired
    private PartnerInsiderSettleLogManager partnerInsiderSettleLogManager;

    @ApiOperation("搜索")
    @PostMapping("/search")
    @ResponseBody
    @PreAuthorize("hasAnyRole('ADMIN', 'partner_insider_settle_log::search')")
    public ResultPagedList<PartnerInsiderSettleLogVo> searchPartnerInsiderSettleLog(@RequestBody PartnerInsiderSettleLogSearchModel searchModel) {

        List<PartnerInsiderSettleLog> records = partnerInsiderSettleLogManager.search(searchModel);
        long count = partnerInsiderSettleLogManager.searchCount(searchModel);

        BigDecimal amount = partnerInsiderSettleLogManager.sumPartnerSettleAmount(searchModel);
        List<PartnerInsiderSettleLogVo> vos = BeanCopierUtil.batchCopy(PartnerInsiderSettleLog.class, PartnerInsiderSettleLogVo.class, records);

        for (PartnerInsiderSettleLogVo vo : vos) {
            vo.setTotalBankAmount(vo.getSuccessSafePayBankAmount());
            vo.setTotalAlipayAmount(vo.getSuccessSafePayAlipayAmount().add(vo.getSuccessPayTransAmount()));
        }

        ResultPagedList<PartnerInsiderSettleLogVo> res = new ResultPagedList<>(vos, count, searchModel);
        res.setExtra(amount.toString());
        return res;
    }
}
